package com.wll.store.user.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wll.store.common.component.JwtComponent;
import com.wll.store.common.config.JWTToken;
import com.wll.store.common.result.PageResult;
import com.wll.store.common.result.Result;
import com.wll.store.common.utils.RedisUtils;
import com.wll.store.user.dto.UserInfo;
import com.wll.store.user.dto.UserPageInfo;
import com.wll.store.user.dto.UserPassword;
import com.wll.store.user.entity.User;
import com.wll.store.user.service.UserService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.UnauthorizedException;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.security.Key;
import java.util.Objects;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author sdx2009
 * @since 2022-09-04
 */
@RestController
@RequestMapping("/api/user")
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private JwtComponent jwtComponent;

    @Autowired
    private RedisTemplate<String, Object> redisTemplate;

    @Autowired
    private RedisUtils redisUtils;

    @PostMapping("/login")
//    @Cacheable(value = "user")
    public Result<?> login(@RequestBody UserInfo userInfo){
        User user = userService.getUserInfoByNameAndPwd(userInfo);
        //登录时根据用户id默认给用户admin角色
        userService.addRoleWithAdmin(user.getUid());

        if (Objects.isNull(user)){
            //异常处理
            throw new UnauthorizedException();
        }
        //生成jwt token
        String jwtToken = jwtComponent.sign(userInfo.getUsername(), userInfo.getPassword(), "web");

        redisUtils.setEx("token",jwtToken,300);

        return new Result<>().success().put(jwtToken);
    }

    @PostMapping("/register")
    public Result<?> register(@RequestBody UserInfo userInfo){
        boolean result = userService.addUserInfo(userInfo);
        if (result){
            return new Result<>().success("用户注册成功");
        }else {
            return new Result<>().error("用户注册失败");
        }
    }



    /*@GetMapping("/page")
    @RequiresAuthentication
    @RequiresRoles("admin")
    public PageResult<User> page(UserPageInfo userPageInfo){
        Page<User> page = userService.getUserPage(userPageInfo);
        PageResult<User> result = new PageResult<>(200, "操作成功", page.getTotal(), page.getRecords(),
                page.getPages(), page.getCurrent());

        //把redis中的token删除
//        redisTemplate.delete("token");
        return result;
    }*/

    @GetMapping("/401")
    public Result<?> unauthorization() {
        return new Result<>().error(401, "没有权限");
    }

    //个人资料
    @GetMapping("/userdata")
    @RequiresAuthentication
    @RequiresRoles("admin")
//    @Cacheable(value = "user")
    public Result<?> userdata(String token){
        JWTToken jwtToken = new JWTToken(token);
        String jwttoken = jwtToken.getPrincipal().toString();
        String username = jwtComponent.getUserNameByToken(jwttoken);
        User user = userService.getUserInfoByName(username);

        if (Objects.isNull(user)){
            //异常处理
            throw new UnauthorizedException();
        }

        return new Result<>().success().put(user);
    }

    //改变个人资料
    @PostMapping("/changeUserinfo")
//    @CachePut(value = "userInfo")
    public Result<?> changeUserinfo(@RequestBody User user){
        boolean result = userService.updateUserInfo(user);
        if (result){
            return new Result<>().success("修改成功");
        }else {
            return new Result<>().error("修改失败");
        }
    }

    @PostMapping("/changePassword")
    @RequiresAuthentication
    @RequiresRoles("admin")
//    @CachePut(value = "userInfo")
    public Result<?> changePassword(@RequestBody UserPassword userPassword){
        //根据用户旧密码获取用户信息
        boolean result = userService.updateUserPassword(userPassword);
        if (result){
            return new Result<>().success("密码修改成功");
        }else {
            return new Result<>().error("密码修改失败");
        }

    }

}
